
define(['jquery','uikit','utils'],function ($,uikit,utils) {

  var timer = "", nums = 60,validCode = true;

  function User() {
    this.showPwd = true;
    this.status = true;
  }

  User.prototype = {
    init () {
      this.changeUserAction()
      this.submitUserLogin()
      this.submitUserAdd()
      this.sendSmsCode()
      this.showPassword()
    },

    /**
     * 切换登录注册按钮被点击
     * true 登录
     * false 注册
     */
    changeUserAction () {
      $(".addUser").click( () => {
        if(this.status) {
          $(".login-user").css({
            display: 'none'
          })
          $(".add-user").css({
            display: 'block'
          })
          $(".addUser").text("登 录")
          this.status = false
        }else {
          $(".login-user").css({ display: 'block' })
          $(".add-user").css({ display: 'none' })
          $(".addUser").text("注 册")
          this.status = true
        }
      });
    },

    /**
     * 用户登录
     */
    submitUserLogin () {
      $(".LoginUser").click( () => {
        var uphone = $(".uphone").val(),
          usmscode  = $(".usmscode").val();
        if(!(usmscode.length < 6) && /^[1][3,4,5,7,8][0-9]{9}$/.test(uphone) ) {

          utils.post({
            url: '/user/login',
            data: {
              userphone: $(".uphone").val(),
              smscode: $('.usmscode').val()
            }
          },function (res){
            if(!(res.hasOwnProperty("methods"))) {
              localStorage.setItem("objectId",res.result[0].objectId )
              location.href = utils.GetQueryString("from")
            }else {
              uikit.notification({
                message: res.result,
                status: 'primary',
                pos: 'top-right',
                timeout: 5000
              });
            }

          })
        }else {
          uikit.notification({
            message: "验证码和手机号不为空",
            status: 'primary',
            pos: 'top-right',
            timeout: 5000
          });
        }
      })
    },

    /**
     * 用户注册
     */
    submitUserAdd(){
      $(".AddUser").click( () => {
        var userphone = $(".addphone").val(),
            username = $(".addname").val(),
            useremail = $(".addemail").val(),
            userpwd = $(".addpwd").val();

        if( /^[1][3,4,5,7,8][0-9]{9}$/.test(userphone) &&
            /^[A-Za-z0-9]{10,20}$/.test(username) &&
            /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(useremail) &&
            /^[A-Za-z0-9]{6,20}$/.test(userpwd)) {

          utils.post({
            url: '/user/addUser',
            data: { username: username,userphone: userphone,useremail: useremail,userpassword: userpwd}
          },function (res) {
            if(typeof res.result == "object") {
              uikit.notification({
                message: "注册成功，请登录..",
                status: 'primary',
                pos: 'top-right',
                timeout: 5000
              });
              $(".addUser").click()

            }else {
              uikit.notification({
                message: res.result,
                status: 'danger',
                pos: 'top-right',
                timeout: 5000
              });
            }
          })

        }else {
          uikit.notification({
            message: "输入不符合规范，请仔细检查",
            status: 'danger',
            pos: 'top-right',
            timeout: 5000
          });
        }
      })
    },

    /**
     * 发送验证码被点击
     */
    sendSmsCode () {
      $(".send-smscode").click( () => {
        var uphone = $(".uphone").val();
        if(/^[1][3,4,5,7,8][0-9]{9}$/.test(uphone) && validCode){
          this.sendSmsCodeAction()

          utils.post({
            url: '/user/sendSmsCode',
            data: {
              userphone: $(".uphone").val()
            }
          },function (res) {
            if( res.hasOwnProperty("methods") ) {
              uikit.notification({
                message: "验证码发送失败，重新发送",
                status: 'danger',
                pos: 'top-right',
                timeout: 5000
              });
            }
          })

        }else {

          uikit.notification({
            message: validCode ? '手机号错误' : '不要重复发送验证码',
            status: 'danger',
            pos: 'top-right',
            timeout: 3000
          });
        }
      })
    },

    /**
     * 具体发送验证码的逻辑
     */
    sendSmsCodeAction () {
      validCode = false;
      uikit.notification({
        message: '验证码已发送，注意查收',
        status: 'danger',
        pos: 'top-right',
        timeout: 3000
      });
      timer=setInterval(function(){
        if(nums>0){
          nums--;
          $(".send-smscode").text(`${nums}s`);
        }
        else{
          clearInterval(timer);
          nums=60;//重置回去
          validCode=true;
          $(".send-smscode").text("发送验证码");
        }
      },1000)
    },

    /**
     * 显示密码被点击
     */
    showPassword () {
      $(".showpwd").click( () => {
        if(this.showPwd) {
          $(".addpwd").attr({
            type: 'text'
          })
          this.showPwd = false
        }else {
          $(".addpwd").attr({
            type: 'password'
          })
          this.showPwd = true
        }
      })
    },

  }

  return new User();
});